package huawei.js2025;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 端口合并
 */
public class Question35 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.nextLine();

        LinkedList<Set<Integer>> ports = new LinkedList<Set<Integer>>();// 端口组
        for (int i = 0; i < N; i++) {
            Set<Integer> portGroup = Arrays.stream(sc.nextLine().split(",")).map(Integer::parseInt).collect(Collectors.toSet());
            ports.add(portGroup);
        }

        while (merge(ports));

        System.out.println(ports);
    }

    public static boolean merge(LinkedList<Set<Integer>> ports) {
        for (int i = 0; i < ports.size(); i++) {
            for (int j = i + 1; j < ports.size(); j++) {
                Set<Integer> set = retainCollection(ports.get(i), ports.get(j));
                if (set.size() >= 2) {
                    Set<Integer> newPorts = new HashSet<>(ports.get(i));
                    newPorts.addAll(ports.get(j));
                    ports.set(i, newPorts);
                    ports.remove(j);
                    return true;
                }
            }
        }
        return false;
    }

    public static Set<Integer> retainCollection(Set<Integer> list1, Set<Integer> list2) {
        Set<Integer> list = new HashSet<>(list1);
        list.retainAll(list2);
        return list;
    }
}
